package com.example.fashionshoes.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fashionshoes.mapper.GoodsMapper;
import com.example.fashionshoes.pojo.dto.GoodsPageDTO;
import com.example.fashionshoes.pojo.po.Goods;
import com.example.fashionshoes.pojo.vo.GoodsDetailVO;
import com.example.fashionshoes.pojo.vo.GoodsPageVO;
import com.example.fashionshoes.service.GoodsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    /**
     * 获取新鲜好物数据
     * @return 新鲜好物数据
     */
    @Override
    public List<Goods> getNewGoods() {
        return lambdaQuery().gt(Goods::getCreateTime,LocalDate.now().plusMonths(-5L)).orderBy(true,false,Goods::getCreateTime).list();
    }
    /**
     * 根据分类id获取商品
     * @param id 分类id
     * @return 分类id对应商品
     */
    @Override
    public List<Goods> getCategoryId(Integer id) {
        return lambdaQuery().eq(Goods::getCategoryId, id).list();
    }

    /**
     * 根据分类id分页获取对应商品
     * @param goodsPageDTO 分类id和分页数据
     * @return 分页对应商品
     */
    @Override
    public GoodsPageVO getSubGoods(GoodsPageDTO goodsPageDTO) {
        Page<Object> objects = PageHelper.startPage(goodsPageDTO.getPage(), goodsPageDTO.getPageSize());
        Page<Goods> page =  goodsMapper.getSubGoods(goodsPageDTO);
        objects.close();
        return GoodsPageVO.builder()
                .count(page.getTotal())
                .pages(page.getPages())
                .pageSize(page.getPageSize())
                .page(page.getPageNum())
                .goodsList(page.getResult())
                .build();
    }

    /**
     * 获取热榜商品
     * @param id 商品id
     * @param type 1为24小时榜，2为周榜
     * @param limit 商品数量
     * @return 热榜商品
     */
    @Override
    public List<GoodsDetailVO> getHotGoods(Integer id, Integer type, Integer limit) {
        Goods goods = getById(id);
        return  goodsMapper.getHotGoods(goods.getCategoryId(),type,limit);
    }

    /**
     * 根据关键词查询商品
     * @param goodsPageDTO 关键词、分页数据等
     * @return 对应商品
     */
    @Override
    public GoodsPageVO search(GoodsPageDTO goodsPageDTO) {
        Page<Object> objects = PageHelper.startPage(goodsPageDTO.getPage(), goodsPageDTO.getPageSize());
        String keyword = goodsPageDTO.getKeyword();
        //初中生，学生，男性。。。。
        if (keyword.length()<=3) {
            if (keyword.contains("儿童")||keyword.contains("小孩")||keyword.contains("学生")||keyword.contains("初中")){
                goodsPageDTO.setType(3);
            }else if (keyword.contains("女")){
                goodsPageDTO.setType(2);
            }else if (keyword.contains("男")){
                goodsPageDTO.setType(1);
            }else {
                //根据名称搜索
            }
        }
        Page<Goods> page = goodsMapper.search(goodsPageDTO);
        objects.close();
        return GoodsPageVO.builder()
                .pages(page.getPages())
                .pageSize(page.getPageSize())
                .goodsList(page.getResult())
                .build();
    }

}
